---
import BaseLayout from "@/layouts/BaseLayout.astro";
import { COLLECT_TAGS } from "@/constant/Collect";
import { getCollection } from "astro:content";
import cfg from 'blog.config'
const ArticleList = await getCollection(
  "blog",
  ({ data }) => data.draft !== true
), {WebsiteSettings:{base}} = cfg;

let tagMap: Record<string, any> = {},
  max = 0,
  min = Infinity;

function SizeFactory(n: number) {
  const len = Math.abs(max - min),
    chunk = len / 4;
  if (chunk * 3 < n) return "xl";
  else if (chunk * 2 < n) return "l";
  else if (chunk * 1 < n) return "m";
  return "s";
}

ArticleList.forEach(({ data: { tags, categories } }) => {
  if (tags) {
    tags.forEach((val) => (tagMap[val] ? tagMap[val]++ : (tagMap[val] = 1)));
    for (let i in tagMap) {
      let n = tagMap[i];
      if (n > max) max = n;
      if (n < min) min = n;
    }
  }
});

const Word = [];

for (const i in tagMap) {
  Word.push({
    content: i,
    size: SizeFactory(tagMap[i]),
  });
}
---

<BaseLayout title={`标签 - `}>
  <div class="tags flex_center">
    <div class="wrapper white_module">
      {
        Object.keys(tagMap).length ? (
          Word.map((val) => {
            return (
              <a
                href={`${base}/collect/${COLLECT_TAGS}/${val.content}`}
                class={`word ${val.size}`}
              >
                {val.content}
              </a>
            );
          })
        ) : (
          <h3>没有更多内容，太伤感了...</h3>
        )
      }
    </div>
  </div>
</BaseLayout>

<style lang="scss">
  .tags {
    margin: 6rem 0;
    .wrapper {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
      align-items: center;
      gap: 2rem;
      width: 70%;
      min-height: 30rem;
      padding: 5rem;
      font-size: 1.4rem;

      .word {
        color: var(--tips-default);
        opacity: 0.75;
        transition: opacity 0.3s;

        &:hover {
          opacity: 1;
        }

        &.s {
          font-size: 0.7em;
        }

        &.m {
          font-size: 0.9em;
        }

        &.l {
          font-size: 1.1em;
        }

        &.xl {
          font-size: 1.4rem;
        }
      }
    }
  }

  @media screen and (max-width: 992px) {
    .tags {
      .wrapper {
        padding: 2rem;
        width: 90%;
      }
    }
  }
</style>
